package cs348a.opengl;

import java.awt.Color;

/**
 * <code>OpenGLLightModel</code> contains the data for OpenGL lighting.
 * TODO: the light values are set in the initialization process 
 * and is not updated in real time. 
 * add code in the display() method of the renderer to reflect the change.
 * 
 * @author H.S Kim
 */
public class JoglLightModel {
	/** The ID number of the light */
	private final int id;
	
	/** The ambient light property */
	private Color ambient = new Color(0.45f, 0.45f, 0.45f, 1.0f);
	/** The diffuse light property */
	private Color diffuse = new Color(0.45f, 0.45f, 0.45f, 1.0f);
	/** The ambient light property */
	private Color specular = new Color(0.9f, 0.9f, 0.9f, 1.0f);
    /** The position of the light */
	private float position[] = {100.0f, 100.0f, 500.0f};
	/** The intensity of the light */
	private float intensity = 0.1f;
	
	/** Enable flag */
	private boolean enable = true;
    
	/**
	 * Default constructor
	 * 
	 * @param id - the ID of the light. e.g) GL.GL_LIGHT0
	 */
	public JoglLightModel(int id){
		this.id = id;
    }
	
	/**
	 * Returns the ID.
	 * 
	 * @return
	 */
	public int id(){
		return id;
	}
	
	/**
	 * Enable/disables the light.
	 * 
	 * @param enable
	 */
	public void setEnabled(boolean enable){
		this.enable = enable;
	}
	
	/**
	 * Is the light enabled.
	 * 
	 * @return
	 */
	public boolean isEnabled(){
		return enable;
	}
	
	/**
	 * Sets the color of the ambient light.
	 * 
	 * @param color
	 */
	public void setAmbientLight(Color color){
		ambient = new Color(color.getRed(), color.getGreen(), 
				color.getBlue(), color.getAlpha());
	}
	
	/**
	 * Returns the color of the ambient light.
	 * 
	 * @return
	 */
	public Color getAmbientLight(){
		return new Color(ambient.getRed(), ambient.getGreen(), 
				ambient.getBlue(), ambient.getAlpha());
	}
	
	/**
	 * Sets the color of the diffused light.
	 * 
	 * @param color
	 */
	public void setDiffuseLight(Color color){
		diffuse = new Color(color.getRed(), color.getGreen(), 
				color.getBlue(), color.getAlpha());
	}

	/**
	 * Returns the color of the diffuse light.
	 * 
	 * @return
	 */
	public Color getDiffuseLight(){
		return new Color(diffuse.getRed(), diffuse.getGreen(), 
				diffuse.getBlue(), diffuse.getAlpha());
	}
	
	/**
	 * Sets the color of the specular light.
	 * 
	 * @param color
	 */
	public void setSpecularLight(Color color){
		specular = new Color(color.getRed(), color.getGreen(), 
				color.getBlue(), color.getAlpha());
	}
	
	/**
	 * Returns the color of the specular light.
	 * 
	 * @return
	 */
	public Color getSpecularLight(){
		return new Color(specular.getRed(), specular.getGreen(), 
				specular.getBlue(), specular.getAlpha());
	}
	
	/**
	 * Sets the position.
	 * 
	 * @param position
	 */
	public void setPosition(float[] position){
		this.position = position.clone();
	}
	
	/**
	 * Returns the position.
	 * 
	 * @return
	 */
	public float[] getPosition(){
		return position.clone();
	}
	
	/**
	 * Sets the intensity.
	 * 
	 * @param intensity
	 */
	public void setIntensity(float intensity){
		this.intensity = intensity; 
	}
	
	/**
	 * Returns the intensity.
	 * 
	 * @return
	 */
	public float getIntensity(){
		return intensity;
	}
	
	/**
	 * Returns the ambient components.
	 * 
	 * @return
	 */
	public float[] ambient(){
		float[] a = new float[4];
		ambient.getComponents(a);	// not sure if this will always be consistent 
									// with values set as R/G/B/A. 
		return a;
	}
	
	/**
	 * Returns the diffuse components.
	 * 
	 * @return
	 */
	public float[] diffuse(){
		float[] d = new float[4];
		diffuse.getComponents(d);	// not sure if this will always be consistent 
									// with values set as R/G/B/A. 
		return d;
	}
	
	/**
	 * Returns the specular components.
	 * 
	 * @return
	 */
	public float[] specular(){
		float[] s = new float[4];
		specular.getComponents(s);	// not sure if this will always be consistent 
									// with values set as R/G/B/A. 
		return s;
	}
	
	/**
	 * Returns the position and intensity as needed in OpenGL
	 * 
	 * @return
	 */
	public float[] position() {
		float[] p = {position[0], position[1], position[2], intensity};
		return p;
	}
}
